package WebSocket和HTML通讯;


import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;


public class WebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        
        pipeline.addLast(new HttpServerCodec());//WebSocket是基于Http，因此要添加此编解码器。
        pipeline.addLast(new ChunkedWriteHandler());//以块(Chunked)的方式写的处理器。
        pipeline.addLast(new HttpObjectAggregator(8192));//将HttpMessage和HttpContent聚合(Aggregator)成FullHttpRequest或FullHttpResponse，8192参数是最大聚合字节数。
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));//访问地址示例：ws://localhost:8899/ws，其中地址最后的/ws就是此函数的参数。Protocol：协议。

        pipeline.addLast(new TextWebSocketFrameHandler());
    }
}
